import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author LKQ
 * @date 2022/3/5 11:08
 * @description 将二维矩阵转换为一维，完成对应的映射
 */
public class Solution {

    Map<Integer, Integer> map = new HashMap<>();
    int m, n;
    int total;
    Random ran = new Random();
    public Solution(int m, int n) {
        this.m = m;
        this.n = n;
        this.total = m * n;
    }
    public int[] flip() {
        int x = ran.nextInt(total);
        total--;
        // 查找x位置对应的映射
        int idx = map.getOrDefault(x,x);
        // 将位置x对应的映射位置修改为total所对应的映射位置
        map.put(x, map.getOrDefault(total, total));
        return new int[]{idx / n, idx % n};
    }
    public void reset() {
        total = m * n;
        map.clear();
    }

    public static void main(String[] args) {
        Solution solution = new Solution(3,1);
        solution.flip();
        solution.flip();
        solution.flip();
        solution.reset();
        solution.flip();
    }
}
